//
//  HMHomeTableViewController.m
//  weibo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMHomeTableViewController.h"
#import "UIBarButtonItem+HMBarButtonItem.h"
#import "HMHomeDemoController.h"

#import "HMHomeViewModel.h"
#import "HMStatusCell.h"
#import "HMHomeRefresh.h"
NSString *orangalID = @"HMStatusCellID";
NSString *transmitID = @"HMtransmitID";
@interface HMHomeTableViewController ()

@property(nonatomic,strong)HMHomeViewModel *homeViewModel;
@property(nonatomic,strong) UILabel *lb_toprefreshePrompt;
//@property(nonatomic,weak)UIRefreshControl *<#name#>;
@property(nonatomic,strong) UIActivityIndicatorView *pullupRefreshView;
@property(nonatomic,strong) HMHomeRefresh *hmrefresh;
@end

@implementation HMHomeTableViewController
-(HMHomeViewModel *)homeViewModel{
    if (!_homeViewModel) {
        _homeViewModel = [[HMHomeViewModel alloc]init];
    }
    return _homeViewModel;
}
-(UILabel *)lb_toprefreshePrompt{
    if (!_lb_toprefreshePrompt) {
        _lb_toprefreshePrompt = [[UILabel alloc]initWithFrame:CGRectMake(0,64 - 30,ScreenWidth , 30)];
        [self.view insertSubview:_lb_toprefreshePrompt belowSubview:self.naviBar];
        _lb_toprefreshePrompt.backgroundColor = [UIColor orangeColor];
        _lb_toprefreshePrompt.font = [UIFont systemFontOfSize:15];
        _lb_toprefreshePrompt.hidden = YES;
    }
    return _lb_toprefreshePrompt;
}
-(UIActivityIndicatorView *)pullupRefreshView{
    if (!_pullupRefreshView) {
        _pullupRefreshView = [[UIActivityIndicatorView alloc]init];
        _pullupRefreshView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    }
    return _pullupRefreshView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.isOnlogin) {
        [self.loginV updateHMLoginViewWithText:@"关注一些人，回到这里看看有什么惊喜" imgName:@"visitordiscover_feed_image_smallicon" isHomeVc:YES];
        return;
    }
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImgName:@"navigationbar_pop" title:@"" Target:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    self.naviItem.rightBarButtonItem = rightItem;
    [self setupUI];
    [self loadData];
}
-(void)push{
    HMHomeDemoController *homeDemoVc = [[HMHomeDemoController alloc]init];
    [self.navigationController pushViewController:homeDemoVc animated:YES];
}
#pragma mark
#pragma mark - 下载数据
-(void)loadData{
    
    [self.homeViewModel lodaHomeMessageispullup:self.pullupRefreshView.animating finishedBlock:^(BOOL isFinished,NSInteger count) {
        if (!isFinished) {
            [SVProgressHUD showErrorWithStatus:networkWarn];
            return ;
        }
        [self addTopRefreshPromtAnimationCount: count];
//        [self.tableView.refreshControl endRefreshing];
        [_hmrefresh endHMHomeRefeshAnimation];
        [_pullupRefreshView stopAnimating];
        [self.tableView reloadData];
    }];
}
#pragma mark
#pragma mark - 下拉刷新的提示动画
-(void)addTopRefreshPromtAnimationCount:(NSInteger)count{
    if (self.lb_toprefreshePrompt.hidden == NO) {
        return;
    }
    NSString *text = @"";
    if (count == 0) {
        text = @"没有刷新的微博了" ;
    }else{
     text = [NSString stringWithFormat:@"刷新了%zd条微博",count];
    }
    self.lb_toprefreshePrompt.text = text;
    self.lb_toprefreshePrompt.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lb_toprefreshePrompt.transform = CGAffineTransformMakeTranslation(0, 30);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
            self.lb_toprefreshePrompt.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            self.lb_toprefreshePrompt.hidden = YES;
        }];
    }];
}

-(void)setupUI{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HMStatusCell" bundle:nil] forCellReuseIdentifier:orangalID];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMtransmit" bundle:nil] forCellReuseIdentifier:transmitID];
    self.tableView.tableFooterView = self.pullupRefreshView;
    
//    UIRefreshControl *refreshC = [[UIRefreshControl alloc]init];
//    self.tableView.refreshControl = refreshC;
    self.hmrefresh = [[HMHomeRefresh alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, 64)];
//    self.hmrefresh = [[HMHomeRefresh alloc]init];
//    [self.view insertSubview:_hmrefresh belowSubview:self.naviBar];
    [self.tableView addSubview:_hmrefresh];
    [_hmrefresh addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
//    self.tableView.rowHeight = 300;
    //self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.homeViewModel.hmstatusArr.count;
    return self.homeViewModel.hmstatusArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   HMStatusViewModel *statusModel =  self.homeViewModel.hmstatusArr[indexPath.row];

    NSString *cellID = (statusModel.status.retweeted_status == nil ? orangalID : transmitID);
    
    HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.statusModel = statusModel;
    
    return cell;
}

#pragma mark
#pragma mark - delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HMStatusViewModel *model = [self.homeViewModel hmstatusArr][indexPath.row];
    
    NSString *nibName = model.status.retweeted_status == nil ? @"HMStatusCell" : @"HMtransmit";
    HMStatusCell *cell = [[[UINib nibWithNibName:nibName bundle:nil]instantiateWithOwner:nil options:nil] lastObject];
    
    cell.statusModel = model;
    [cell layoutIfNeeded];

    return CGRectGetMaxY(cell.vi_bottomTool.frame);
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!_pullupRefreshView.animating && (self.homeViewModel.hmstatusArr.count - 2 == indexPath.row)) {
        [_pullupRefreshView startAnimating];
        [self loadData];
    }

}
@end
